//app.js
App({
  onLaunch: function () {   
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo:function(cb){
    var that = this
    console.log(this.globalData.userInfo)
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function (res) {
          var code = res.code;
          var userInfo = res.userInfo

          if (code) {
            console.log('获取用户登录凭证：' + code);
            console.log(res)
            // --------- 发送凭证用户信息-------------
            wx.request({
              url: '',
              header: {
                'content-Type': 'application/json'
              },
              method:'POST',
              data: { code: code, userInfo: userInfo}
            })
            // ------------------------------------
          } else {
            console.log(res)
            console.log('获取用户登录凭证：' + code);            
            console.log('获取用户登录态失败：' + res.errMsg);
          }
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  onShow: function () {    
  },
  onHide: function () {   
  },
  globalData:{
    userInfo:null
  }
})